package com.itheima.mm.controller;

import com.itheima.mm.base.BaseController;
import com.itheima.mm.common.GlobalConst;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Catalog;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.CatalogService;
import com.itheima.mm.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ShenHD
 * @Date 2021/8/11
 */
@RestController
@RequestMapping("/catalog")
@Slf4j
public class CatalogController extends BaseController {

    @Autowired
    private CatalogService catalogService;
@PostMapping("/add")
    public Result addCatalog(HttpServletRequest request, @RequestBody  Catalog catalog) {
        User user = getSessionUser(request, GlobalConst.SESSION_KEY_USER);
        if (user != null) {
            catalog.setUserId(user.getId());
        } else {
            catalog.setUserId(1);
        }
        catalog.setStatus(0);
        catalog.setCreateDate(DateUtils.parseDate2String(new Date()));
        catalogService.addCatalog(catalog);
        return new Result((true), "添加目录成功");
    }
@PostMapping("/findListByPage")
    public Result findListByPage( @RequestBody QueryPageBean pageBean) {
        if (pageBean == null) {
             pageBean = new QueryPageBean();
            pageBean.setCurrentPage(1);
            pageBean.setPageSize(10);
        }
        log.info("findListByPage questionList pageBean:{}",pageBean);
        PageResult pageResult = catalogService.findListByPage(pageBean);
        return new Result(true, "获取学科目录列表成功", pageResult);
    }

    public Result deleteCatalog(String catalogId) {
        try {
            log.info("deleteCatalog id", catalogId);
            catalogService.deleteCatalog(Integer.parseInt(catalogId));
            return new Result((true), "删除学科目录成功");
        } catch (NumberFormatException e) {
            log.error("deleteCatalog", e);
            return new Result((false), e.getMessage());
        }
    }
}
